using System;
using System.Windows.Forms;
using WorkOrderPrinter.Models;
using WorkOrderPrinter.Services;

namespace WorkOrderPrinter
{
    public partial class TemplateManagementForm : Form
    {
        private readonly ITemplateService _templateService;
        private PrintTemplate _currentTemplate;

        public TemplateManagementForm(ITemplateService templateService)
        {
            InitializeComponent();
            _templateService = templateService;
        }

        private void TemplateManagementForm_Load(object sender, EventArgs e)
        {
            LoadTemplates();
        }

        private void LoadTemplates()
        {
            var templates = _templateService.GetAllTemplates();
            lstTemplates.Items.Clear();
            
            foreach (var template in templates)
            {
                lstTemplates.Items.Add(template);
            }
            
            if (lstTemplates.Items.Count > 0)
            {
                lstTemplates.SelectedIndex = 0;
            }
        }

        private void lstTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstTemplates.SelectedItem is PrintTemplate template)
            {
                _currentTemplate = template;
                UpdateFormWithTemplate(template);
            }
        }

        private void UpdateFormWithTemplate(PrintTemplate template)
        {
            txtTemplateName.Text = template.Name ?? string.Empty;
            txtModelFilter.Text = template.Model ?? string.Empty;
            txtBatchPattern.Text = template.BatchPattern ?? string.Empty;
            txtTemplateContent.Text = template.Content ?? string.Empty;
            chkIsDefault.Checked = template.IsDefault;
            
            // 新增的属性
            txtDescription.Text = template.Description ?? string.Empty;
            txtProductName.Text = template.ProductName ?? string.Empty;
            txtProductCapacity.Text = template.ProductCapacity ?? string.Empty;
            txtProductVoltage.Text = template.ProductVoltage ?? string.Empty;
            txtTemperatureUnit.Text = template.TemperatureUnit ?? string.Empty;
            txtProductSeries.Text = template.ProductSeries ?? string.Empty;
            txtProductNumber.Text = template.ProductNumber ?? string.Empty;
        }

        private PrintTemplate GetTemplateFromForm()
        {
            var template = _currentTemplate ?? new PrintTemplate();
            
            template.Name = txtTemplateName.Text.Trim();
            template.Model = txtModelFilter.Text.Trim();
            template.BatchPattern = txtBatchPattern.Text.Trim();
            template.Content = txtTemplateContent.Text;
            template.IsDefault = chkIsDefault.Checked;
            
            // 新增的属性
            template.Description = txtDescription.Text.Trim();
            template.ProductName = txtProductName.Text.Trim();
            template.ProductCapacity = txtProductCapacity.Text.Trim();
            template.ProductVoltage = txtProductVoltage.Text.Trim();
            template.TemperatureUnit = txtTemperatureUnit.Text.Trim();
            template.ProductSeries = txtProductSeries.Text.Trim();
            template.ProductNumber = txtProductNumber.Text.Trim();
            
            return template;
        }

        private void btnSaveTemplate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTemplateName.Text.Trim()))
            {
                MessageBox.Show("请输入模板名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (string.IsNullOrEmpty(txtTemplateContent.Text.Trim()))
            {
                MessageBox.Show("请输入模板内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var template = GetTemplateFromForm();
            bool success = _templateService.SaveTemplate(template);
            
            if (success)
            {
                MessageBox.Show("模板保存成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                LoadTemplates();
            }
            else
            {
                MessageBox.Show("模板保存失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnNewTemplate_Click(object sender, EventArgs e)
        {
            _currentTemplate = null;
            txtTemplateName.Clear();
            txtModelFilter.Clear();
            txtBatchPattern.Clear();
            txtTemplateContent.Clear();
            chkIsDefault.Checked = false;
            lstTemplates.SelectedIndex = -1;
            
            // 清空新增的属性
            txtDescription.Clear();
            txtProductName.Clear();
            txtProductCapacity.Clear();
            txtProductVoltage.Clear();
            txtTemperatureUnit.Clear();
            txtProductSeries.Clear();
            txtProductNumber.Clear();
            
            txtTemplateName.Focus();
        }

        private void btnDeleteTemplate_Click(object sender, EventArgs e)
        {
            if (_currentTemplate == null)
            {
                MessageBox.Show("请选择要删除的模板", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show($"确定要删除模板 \"{_currentTemplate.Name}\" 吗？", "确认", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                bool success = _templateService.DeleteTemplate(_currentTemplate.Id);
                
                if (success)
                {
                    MessageBox.Show("模板删除成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadTemplates();
                }
                else
                {
                    MessageBox.Show("模板删除失败，不能删除最后一个模板", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            string helpText = "模板内容中可以使用以下占位符，打印时会自动替换为实际值：\r\n" +
                              "{Model} - 型号\r\n" +
                              "{Batch} - 批次\r\n" +
                              "{Date} - 日期\r\n" +
                              "{InitialSerial} - 起始序号\r\n" +
                              "{Quantity} - 数量\r\n" +
                              "{Notes} - 备注\r\n\r\n" +
                              "型号和批次模式用于自动匹配模板，留空表示匹配所有。";
            
            MessageBox.Show(helpText, "模板帮助", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
